project(Origin LANGUAGES C)

set(CMAKE_EXE_LINKER_FLAGS "-m elf_i386 -T ${PROJECT_SOURCE_DIR}/Linker.lds")
set(CMAKE_C_LINK_EXECUTABLE "${LINKER_TOOL} <OBJECTS> ${CMAKE_EXE_LINKER_FLAGS} -o ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.elf")

# 将所有的汇编、C文件加入工程
# 注意保证start.asm在最前头
file(GLOB_RECURSE FILE_LIST "*.c" "*.h" "*.S")
add_executable(${PROJECT_NAME} 
    Origin.S
    core/Irq.S
    core/Global.c 
    ${FILE_LIST}
    )

# 这里是因为要自定义节区空间的分配，然后把对应的节区加载到对应的位置，所以会生成elf文件，保留elf信息
add_custom_command(TARGET ${PROJECT_NAME}
                   POST_BUILD
                   COMMAND ${OBJCOPY} -S ${PROJECT_NAME}.elf ${CMAKE_SOURCE_DIR}/bin/${PROJECT_NAME}.elf
                   COMMAND ${OBJDUMP} -x -d -S -m i386 ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.elf > ${PROJECT_NAME}_dis.txt
                   COMMAND ${READELF} -a ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.elf > ${PROJECT_NAME}_elf.txt
)